#include "myvex.h"
#include "myline.h"

#include <QDebug>
#include <QMenu>
#include <QGraphicsScene>
#include <QGraphicsSceneContextMenuEvent>
#include <QMoveEvent>

MyVex::MyVex(int Vno,QMenu *itemMenu,QString str,QGraphicsItem *parent)
    :QGraphicsEllipseItem(0,0,25,25,parent)
{
    this->Vno = Vno;
    this->index = Vno-1;
    this->itemMenu = itemMenu;
    this->info = str;
    this->txt = new QGraphicsTextItem(QString::number(Vno),this);
    this->brush = new QBrush(Qt::cyan);
    this->setBrush(*brush);
    this->setFlags(QGraphicsItem::ItemIsMovable
                 |QGraphicsItem::ItemIsSelectable
                 |QGraphicsItem::ItemSendsGeometryChanges);

}

MyVex::~MyVex()
{   //qDebug()<<"顶点析构 出链"<<outlines.size();
    delete txt;
    delete brush;
    while(outlines.size()>0) delete outlines.last();
    while(inlines.size()>0) delete inlines.last();
//    for(int i = 0;i<outlines.size();i++) {delete outlines[i];qDebug("第%d条",i+1);}
//    for(int i = 0;i<inlines.size();i++) delete inlines[i];
}

QVariant MyVex::itemChange(GraphicsItemChange change
                           , const QVariant &value)
{
    if(change == QGraphicsItem::ItemPositionChange&&this->scene()){
        for(MyLine *line :qAsConst(outlines))
            line->UpdatePos();
        for(MyLine *line :qAsConst(inlines))
            line->UpdatePos();
    }
    if(itemMenu->actions().size()==2 && change==QGraphicsItem::ItemSelectedChange){
        if(this->isSelected()){
            itemMenu->actions()[0]->triggered();qDebug()  << "lost";}//
    }
    return value;
}

void MyVex::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    if(itemMenu->actions().size()>2) scene()->clearSelection();
    this->setSelected(true);
    itemMenu->exec(event->screenPos());
}

void MyVex::setVirtual()
{
    this->brush->setStyle(Qt::Dense4Pattern);
    this->setBrush(*brush);
    this->setPen(Qt::DashLine);
}

void MyVex::setSolid()
{
    this->brush->setStyle(Qt::SolidPattern);
    this->setBrush(*brush);
    this->setPen(Qt::SolidLine);
}

void MyVex::showInfo(bool y)
{
    if(y){
        delete this->txt;
        this->txt = new QGraphicsTextItem(QString::number(Vno)+info,this);
    }
    else{
        delete this->txt;
        this->txt = new QGraphicsTextItem(QString::number(Vno),this);
    }
}
